
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.Math.EC.Rfc7748;
using QQZiFramework;
using QQZiFramework.MQ;

namespace TestProject1
{
    [TestClass]
    public class TestRedis
    {
        DBContext Context;
        public TestRedis()
        {
            var _OEMProgram = new OEMProgram("JPAAS");
            _OEMProgram.Run(null);
            Context = _OEMProgram.CreateDBContext();


        }

        [TestMethod]
        public void TestRedisMQ()
        {
            RedisMQ redisMQ = new RedisMQ();
            bool isRecive = false;
            redisMQ.BindReceived("testMQ", (b) =>
            {
                isRecive = true;
                return true;
            });
            redisMQ.Publish("testMQ","testMQ");
            DateTime start = DateTime.Now;
            while (true)
            {
                if (isRecive) break;
                Thread.Sleep(200);
                if ((DateTime.Now - start).TotalSeconds > 5)
                {
                    break;
                }
            }
            Assert.IsTrue(isRecive);
        }

        [TestMethod]
        public void TestRedisMQAsync()
        {
            RedisMQ redisMQ = new RedisMQ();
            bool isRecive = false;
            redisMQ.BindReceivedAsync("testMQ", async (b) =>
            {
                await Task.Delay(100);
                isRecive = true;
                return true;
            });
            redisMQ.Publish("testMQ", "testMQ");
            DateTime start = DateTime.Now;
            while(true)
            {
                if (isRecive) break;
                Thread.Sleep(200);
                if((DateTime.Now - start).TotalSeconds > 5)
                {
                    break;
                }
            }
            Assert.IsTrue(isRecive);

        }

        [TestMethod]
        public void TestHashGet()
        {
            var items = Context.GetRedisDatabase(5).HashFind<object>("BizRestore:Variables", "*", 100);
            Console.WriteLine(items);
        }

        [TestMethod]
        public void TestMulitTasks()
        {
            // �����̳߳ص�����߳���
            ThreadPool.SetMaxThreads(50, 50);
            Task[] tasks = new Task[100];
            for (int i = 0; i < tasks.Length; i++)
            {
                tasks[i] = System.Threading.Tasks.Task.Run(() =>
                {
                    using (var newDBContext = Context.Membership.CreateDBContext())
                    {
                        var name = System.Threading.Thread.CurrentThread.ManagedThreadId;
                        for (var j = 0; j < 50; j++)
                        {
                            var key = "Test:Index[" + name + "," + j + "]";
                            var val = "value[" + name + "," + j + "]";
                            newDBContext.GetRedisDatabase(0).SetRedisItem(key, "value" + val);
                            newDBContext.GetRedisDatabase(5).SetRedisItem(key, "value" + val);
                            var ss = newDBContext.GetRedisDatabase(0).GetRedisItem<string>(key, true);
                            var ss2 = newDBContext.GetRedisDatabase(5).GetRedisItem<string>(key, true);
                            Assert.IsTrue(ss.Equals(ss2));
                        }
                    }
                }
                );
            }
            Task.WaitAll(tasks);
        }
    }
}